﻿using System.Configuration;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using RAD.LightOne;
using RAD.LightOne.Data;

using System.Data.SqlClient;
using System.Diagnostics;
using System.Transactions;

//using Petrobras.Geal.Monitoracao;
//using Petrobras.Geal.Monitoracao;
//using MySql.Data.MySqlClient;
//using IBM.Data.DB2;


namespace RAD.LightOne.Tester
{
    class Program
    {
        [DynamicClass("Bananarama")]
        static void Main(string[] args)
        {


            //if (EntityOne.FindAll().Length == 0)
            //{
            //    for (int i = 0; i < 10000; i++)
            //    {
            //        EntityOne e1 = new EntityOne();
            //        e1.StringData = i.ToString();
            //        e1.IntData = i;
            //        e1.Save();
            //    }
            //}

            //Console.WriteLine("Current: {0}", Transaction.Current);
            //
            //Insert
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable }))
            {
                try
                {
                    Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);
                    Console.WriteLine("Current: {0}", Transaction.Current);
                    EntityOne e1 = new EntityOne();
                    e1.StringData = "X1";
                    e1.IntData = 999999;

                    EntityOne e2 = new EntityOne();
                    e2.StringData = "X2";
                    e2.IntData = 999998;

                    e1.Save();
                    e2.Save();

                    scope.Complete();
                    Console.WriteLine("Terminou");

                }
                catch (Exception except)
                {
                    Console.WriteLine(except.Message);
                }
            }

            ////update
            //using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable }))
            //{
            //    try
            //    {
            //        Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);
            //        Console.WriteLine("Current: {0}", Transaction.Current);
            //        Country c1 = Country.FindAll()[0];
            //        c1.Code = "+1";

            //        c1.Save();

            //        new Country()
            //        {
            //            Name = "Brasil",
            //            Code = "+55"
            //        }.Save();

            //        scope.Complete();
            //        Console.WriteLine("Terminou");

            //    }
            //    catch (Exception except)
            //    {
            //        Console.WriteLine(except.Message);
            //    }
            //}

            dynamic din = null;

            try
            {
                DBEngine dbe = DBEngine.GetInstance();
                var count = Convert.ToInt32(dbe.ExecuteScalar(@"SELECT COUNT(*) FROM Entity1"));
            }
            catch
            { }

            try
            {
                var response = DynamicDataFactory.Select(@"select * from Entity1 order by Data1");

                din = response;
            }
            catch { }

            foreach (var r in din)
            {
                Console.WriteLine(r.data1);
            }
            Console.ReadLine();
        }

        static void Current_TransactionCompleted(object sender, TransactionEventArgs e)
        {
            Console.WriteLine(e.Transaction.TransactionInformation.Status);
        }

        static void s_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Console.WriteLine(e.PropertyName);
        }

        public enum MeuEnum
        {
            A = 1,

            B = 2,

            C = 3
        }

        [DBTable("radix")]
        public class Radix : MyObject<Radix>, IFull<Radix>
        {
            [DBField("nome")]
            public string Nome;
            [DBField("valor")]
            public int valor;
            [DBField("minha_opcao")]
            public MeuEnum opcao;


            public static Radix[] FindByValor(int val)
            {
                return Radix.FindBySql("select id, class_name from radix where valor={0}", val);
            }

            public new void Save()
            {
                base.Save();
            }

            public new void Save(IDbTransaction trx)
            {
                base.Save(trx);
            }

            public new void Load()
            {
                base.Load();
            }

            public new void Load(IDbTransaction trx)
            {
                base.Load(trx);
            }

            public new void Delete()
            {
                base.Delete();
            }

            public new void Delete(IDbTransaction trx)
            {
                base.Delete(trx);
            }
        }
    }
}